package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.list.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gsy
 * @date 2021年09月05日13:10
 * Description
 */
@Controller
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list.html")
    public String search(SearchParam searchParam, Model model) {
        //远程调用商品列表客户端获取数据
        SearchResponseVo searchResponseVo = listFeignClient.search(searchParam);
        model.addAttribute("searchParam", searchParam);
        //品牌列表集合
        List<SearchResponseTmVo> trademarkList = searchResponseVo.getTrademarkList();
        model.addAttribute("trademarkList", trademarkList);
        //平台属性列表集合
        List<SearchResponseAttrVo> attrsList = searchResponseVo.getAttrsList();
        model.addAttribute("attrsList", attrsList);
        //商品列表
        List<Goods> goodsList = searchResponseVo.getGoodsList();
        model.addAttribute("goodsList", goodsList);
        //当前页数
        model.addAttribute("pageNo", searchResponseVo.getPageNo());
        //总页数
        model.addAttribute("totalPages", searchResponseVo.getTotalPages());
        //urlParam
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //回显选中的品牌
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //回显选中的平台属性
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);
        //回显排序 排序规则   order=1:asc   1：综合排序/热点  2：价格
        Map orderMap =buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        return "list/index";

    }

    private Map buildOrderMap(SearchParam searchParam) {
        // 排序规则   order=1:asc   1：综合排序/热点  2：价格
        String order = searchParam.getOrder();
        Map map = new HashMap();
        if (!StringUtils.isEmpty(order)){
            String[] p = order.split(":");
            map.put("type",p[0]);
            map.put("sort",p[1]);
            return map;
        }else {
            //走默认综合排序
           map.put("type","1");
           map.put("sort","desc");
        }
        return map;
    }

    //回显选中的平台属性
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        String[] props = searchParam.getProps();
        if (props!=null&&props.length>0){
         return Arrays.stream(props).map(pro->{
             Map map = new HashMap();
             String[] split = pro.split(":");//props=23:4G:运行内存
             map.put("attrName",split[2]);
             map.put("attrValue",split[1]);
             map.put("attrId",split[0]);
             return map;
         }).collect(Collectors.toList());
        }

        return null;
    }

    //回显选中的品牌
    private String buildTrademarkParam(SearchParam searchParam) {
        //回显样式  品牌:品牌名称
        String trademark = searchParam.getTrademark();//trademark=2:华为
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            return "品牌:" + split[1];
        }

        return null;

    }

    //路径拼接参数
    private String buildUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder();
        //关键词
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            stringBuilder.append("http://list.gmall.com/list.html?keyword=").append(searchParam.getKeyword());
        }
        //品牌
        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            stringBuilder.append("&trademark=").append(searchParam.getTrademark());
        }
        //TODO 三级分类

        //平台属性
        if (searchParam.getProps() != null && searchParam.getProps().length > 0) {
            for (String prop : searchParam.getProps()) {
                stringBuilder.append("&props=").append(prop);
            }
        }
        return stringBuilder.toString();
    }
}
